<?php

session_start();

require_once("includes/connect.php");
require_once("includes/function.php");

$pet_id = $_REQUEST["pet_id"];
//$pet_id = "8-2556";
//$panngan2 = $_REQUEST["panngan2"];
//$panngan3 = $_REQUEST["panngan3"];
//$ex1_id = $_REQUEST["ex1_id"];
//$ex2_id = $_REQUEST["ex2_id"];
//$ex3_id = $_REQUEST["ex3_id"];
//$month_year = $_REQUEST["month_year"];

$db = new myDBS;
$Cur = new Currency;

$sql = "SELECT tbl_welfare.*, expenses1_name, expenses2_name, expenses3_name, panngan2_name "
        . " , panngan3_name, panngan2.panngan2_id, panngan3.panngan3_id, expenses_lvl3.e_id, verify_date, finance_id "
        . " FROM tbl_welfare, expenses_lvl1, expenses_lvl2, expenses_lvl3, panngan2, panngan3  "
        . " WHERE pet_id  = '$pet_id' "
        . " and expenses_lvl1.expenses1_id = ex1_id "
        . " and expenses_lvl2.expenses2_id = ex2_id "
        . " and expenses_lvl3.expenses3_id = ex3_id "
        . " and panngan2.panngan2_id = tbl_welfare.panngan2_id "
        . " and panngan3.panngan3_id = tbl_welfare.panngan3_id";
//echo $sql;
$result = $db->runSQL($sql);
$rows = $db->fetch_object($result);

$sql = "SELECT sum(welfare) AS welfare FROM tbl_welfare WHERE pet_id  = '$pet_id' ";

$result1 = $db->runSQL($sql);
$rows1 = $db->fetch_object($result1);

$elass = gen_account_id($rows->panngan2_id, $rows->panngan3_id, $rows->e_id);
// just require TCPDF instead of FPDF
//define('FPDF_FONTPATH','fpdf/font/');
require_once('fpdf/TCPDF/tcpdf.php');
require_once('fpdf/FPDI/fpdi.php');

class PDF extends FPDI {

    /**
     * "Remembers" the template id of the imported page
     */
    var $_tplIdx;

    /**
     * include a background template for every page
     */
    function Header() {
        if (is_null($this->_tplIdx)) {
            $this->numPages = $this->setSourceFile('form/welfare.pdf');

            $this->_tplIdx = $this->importPage(1);
        }
        $this->useTemplate($this->_tplIdx);

        /*
          //$this->AddFont('THSarabunNew','','THSarabunNew.php');
          //$this->SetFont('THSarabunNew', '', 9);
          $this->SetTextColor(255);
          $this->SetXY(60.5, 24.8);
          $this->Cell(0, 8.6, "TCPDF and FPDI");
         */
    }

    function Footer() {
        
    }

}

// initiate PDF
$pdf = new PDF($orientation = 'P', $unit = 'mm', "A4", $unicode = true, $encoding = 'UTF-8', $diskcache = false, $pdfa = false);
$pdf->SetMargins(PDF_MARGIN_LEFT, 40, PDF_MARGIN_RIGHT);
$pdf->SetAutoPageBreak(true, 40);
$pdf->setFontSubsetting(false);

// add a page
$pdf->AddPage();

//$pdf->AddFont('THSarabunNew','','THSarabunNew.php');
//$pdf->SetFont("freeserif", "", 12);
$pdf->SetFont('thsarabunb', 'B', 15);
// now write some text above the imported page
$pdf->SetXY(30, 15);
//$pdf->Write(0, $_SESSION["lvl1"]);
$pdf->Write(0, "สำนักงานเทศบาลนครอุดรธานี");

$pdf->SetXY(28, 21);
$pdf->Write(0, $rows->budget_year);

$pdf->SetXY(92, 22);
$pdf->Write(0, $rows->pet_id);

$pdf->SetXY(162, 29 + $vertical_align);
if (!empty($rows->verify_date)) 
    $pdf->Write(0, date_format(date_create($rows->verify_date), "Y-m-d"));

$pdf->SetXY(24, 29);
$pdf->Write(0, $rows->panngan2_name);

$pdf->SetXY(160, 36.5);
$pdf->Write(0, $elass);

$pdf->SetXY(84, 29);
$pdf->Write(0, $rows->panngan3_name);

$pdf->SetXY(157, 22 + $vertical_align);
$pdf->Write(0, $rows->finance_id);


//$pdf->SetXY(155, 44);
//$pdf->Write(0, $elass);


$pdf->SetXY(28, 35);
//$pdf->Write(0, );
$pdf->MultiCell(50, 10, $rows->expenses1_name, 0, '', 0, 0, '', '', true);

$pdf->SetXY(90, 35);
$pdf->Write(0, $rows->expenses2_name);

$pdf->SetXY(90, 43);
$pdf->Write(0, $rows->expenses3_name);

$pdf->SetXY(76, 57);
$pdf->Cell(41, 0, number_format($rows1->welfare, 2), 0, 0, 'R');

$pdf->SetXY(120, 57);
//$pdf->Write(0, $rows->comment, '', 0, 'J', true, 0, false, true, 0);
//$pdf->Cell(89,48, $rows->comment,0,	1,"L");
$pdf->MultiCell(90, 45, $_SESSION["lvl2"] . " ประจำ " . $rows->month_year, 0, '', 0, 1, '', '', true);

/*
  $pdf->SetXY(73, 76);
  $pdf->Cell(41,0,number_format($rows->inpValues * $rows->inpTax / 100, 2),0,0,'R');

  $pdf->SetXY(73, 83);
  $pdf->Cell(41,0,number_format($rows->inpTotal, 2),0,0,'R');

  $pdf->SetXY(73, 90);
  $pdf->Cell(41,0,number_format($rows->inpValues * $rows->WithholdingTax / 100, 2),0,0,'R');

  $pdf->SetXY(73, 97);
  $pdf->Cell(41,0,number_format($rows->inpPenalty, 2),0,0,'R');
 */
$pdf->SetXY(76, 101);
$pdf->Cell(41, 0, number_format($rows1->welfare, 2), 0, 0, 'R');

$pdf->SetXY(56, 108);
$pdf->Write(0, $Cur->bahtThai($rows1->welfare));


$pdf->SetXY(22, 163);
$pdf->Cell(39, 0, number_format($rows1->welfare, 2), 0, 0, 'C');

$pdf->SetXY(90, 163);
$pdf->Cell(39, 0, number_format($rows1->welfare, 2), 0, 0, 'C');

$pdf->SetXY(157, 163);
$pdf->Cell(39, 0, number_format($rows1->welfare, 2), 0, 0, 'C');

$pdf->SetXY(29, 220);
$pdf->Cell(36, 0, number_format($rows1->welfare, 2), 0, 0, 'C');

$pdf->SetXY(73, 220);
$pdf->Write(0, $Cur->bahtThai($rows1->welfare));
/*
  $pdf->SetXY(27, 233);
  $pdf->Write(0, $rows->company_name);

  $pdf->SetXY(35, 247);
  $pdf->Cell(55,0,number_format($rows->inpNet, 2),0,0,'C');
 */

if ($pdf->numPages > 1) {
    for ($pages = 2; $pages <= $pdf->numPages; $pages++) {
        $pdf->endPage();
        $pdf->_tplIdx = $pdf->importPage($pages);
        $pdf->AddPage();

        switch ($pages) {
            case 2:


                $pdf->SetXY(30, 60);
                $pdf->Write(0, $_SESSION["lvl1"]);

                $pdf->SetXY(30, 70);
                $pdf->Write(0, $_SESSION["lvl2"] . " " . $_SESSION["lvl3"] . " " . $_SESSION["lvl4"]);

                $pdf->SetXY(30, 80);
                $pdf->Write(0, $rows->pet_id);

                $pdf->SetXY(70, 91);
                $pdf->Write(0, number_format($rows1->welfare, 2));

                $pdf->SetXY(110, 125);
                $pdf->Cell(25, 0, number_format($rows1->welfare, 2), 0, 0, 'R');

                $pdf->SetXY(162, 125);
                $pdf->Cell(19, 0, number_format($rows1->welfare, 2), 0, 0, 'R');

                $pdf->SetXY(110, 200);
                $pdf->Cell(25, 0, number_format($rows1->welfare, 2), 0, 0, 'R');

                $pdf->SetXY(162, 200);
                $pdf->Cell(19, 0, number_format($rows1->welfare, 2), 0, 0, 'R');

                //$pdf->Write(0, number_format($rows1->salary,2));

                break;
            case 3:
                $pdf->SetXY(112, 14);
                $pdf->Write(0, $rows->expenses2_name);

                $pdf->SetXY(95, 24);
                $pdf->Write(0, $rows->month_year);

                $pdf->SetXY(100, 34);
                $pdf->Write(0, $rows->pet_id);

                $pdf->SetXY(90, 54);
                $pdf->Write(0, $rows->expenses2_name);

                $pdf->SetXY(152, 46);
                //$pdf->Write(0, $rows->expenses2_name);
                $pdf->MultiCell(30, 17, $rows->expenses2_name, 0, '', 0, 1, '', '', true);

                $db1 = NEW myDBS();
                $db1->selectDB("hr_udoncity");

                $sql = "select * from person where id='" . $rows->id . "'";
                $result3 = $db1->runSQL($sql);
                $rows3 = $db1->fetch_object($result3);

                $pdf->SetXY(20, 62);
                $pdf->Write(0, $rows3->fname . "   " . $rows3->lname);

                $pdf->SetXY(151, 62);
                $pdf->Cell(30, 0, number_format($rows1->welfare, 2), 0, 0, 'R');

                $i = 0;
                while ($rows = $db->fetch_object($result1)) {
                    $i+=10;

                    $sql = "select * from person where id='" . $rows->id . "'";
                    $result3 = $db1->runSQL($sql);
                    $rows3 = $db1->fetch_object($result3);

                    $pdf->SetXY(20, 62 + $i);
                    $pdf->Write(0, $rows3->fname . "   " . $rows3->lname);
                }

                /*
                  $sql= "SELECT * FROM tbl_salary WHERE pet_id  = '$pet_id' ";

                  $result2 = $db->runSQL($sql);


                  $i=0;
                  while($rows2 = $db->fetch_object ( $result2 )) {

                  $sql = "select * from person where id='". $rows2->id ."'";
                  $result3 = $db1->runSQL($sql);
                  $rows3 = $db1->fetch_object ( $result3 );
                  $pdf->SetXY(31, 123 + $i);
                  $pdf->Write(0, $rows3->fname . "   " .$rows3->lname);
                  $i+=7;

                  }

                  $i=0;
                  $pdf->SetXY(125, 123 + $i);
                  $pdf->Cell(24,0,number_format($rows->salary, 2),0,0,'R');
                  while($rows = $db->fetch_object ( $result )) {
                  $i+=7;
                  $pdf->SetXY(125, 123 + $i);
                  $pdf->Cell(24,0,number_format($rows->salary, 2),0,0,'R');

                  }
                 */
                break;
        }
    }
}

$pdf->Output('output_petition1.pdf', 'I');
?>